package com.ca.ridersclubapplication;

import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ca.ridersclubapplication.adapter.ClubAdapter;
import com.ca.ridersclubapplication.request.AfterExecutor;
import com.ca.ridersclubapplication.request.ClubRequester;
import com.ca.ridersclubapplication.request.dto.ClubDto;
import com.ca.ridersclubapplication.request.dto.ClubItemDto;
import com.ca.ridersclubapplication.request.dto.Result;

import java.util.ArrayList;
import java.util.List;

public class ClubListActivity extends ImmersiveActivity {

    private String classify;

    private boolean isFollow = false;
    private Long id;

    private ImageView backView;
    private TextView titleView;

    private RecyclerView rv;
    private ClubAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_list);

        initData();

        initView();

        initListener();

        loadData();
    }

    private void initData() {
        classify = getIntent().getStringExtra("classify");
        if (classify.trim().isEmpty()) {
            Toast.makeText(this, "操作失败", Toast.LENGTH_SHORT).show();
            finishAfterTransition();
        }

        if (classify.contains("follow:")) {
            isFollow = true;
            try {
                id = Long.parseLong(classify.substring(classify.indexOf(":") + 1));
            }
            catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(this, "操作失败", Toast.LENGTH_SHORT).show();
                finishAfterTransition();
            }
        }
    }

    private void initView() {

        backView = findViewById(R.id.iv_back);
        titleView = findViewById(R.id.tv_title);

        if (isFollow) {
            titleView.setText("关注列表");
        }
        else {
            titleView.setText(classify);
        }

        rv = findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ClubAdapter(this, null);
        rv.setAdapter(adapter);
    }

    private void initListener() {

        backView.setOnClickListener(v -> {
            finishAfterTransition();
        });
    }

    private void loadData() {

        if (isFollow) {
            ClubRequester.listFollowClub(this, id, new AfterExecutor<List<ClubDto>>() {
                @Override
                public void execute(Result<List<ClubDto>> result) {
                    List<ClubDto> data = result.getData();
                    if (result.isSuccess() && data != null) {
                        List<ClubItemDto> l = new ArrayList<>();
                        for (ClubDto dto : data) {
                            l.add(ClubItemDto
                                    .builder()
                                    .id(dto.getId())
                                    .name(dto.getName())
                                    .paperCount(dto.getPaperCount())
                                    .build());
                        }
                        adapter.setList(l);
                    }
                }
            });
        }
        else {
            ClubRequester.listClubByClassify(this, classify, new AfterExecutor<List<ClubItemDto>>() {
                @Override
                public void execute(Result<List<ClubItemDto>> result) {
                    List<ClubItemDto> data = result.getData();
                    if (result.isSuccess() && data != null) {
                        adapter.setList(data);
                    }
                }
            });
        }
    }
}